<?php 
namespace App\Http\Controllers\TongGaoApi;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;


use \PhpOffice\PhpWord\PhpWord;
use App\Http\Controllers\TongGaoApi\MergeFile;
//use Illuminate\Http\Response;
class WordActionController extends Controller
{	
	const ROUTE_PREFIX = '/api/*';
	protected $request;
	protected $curTime;
	protected $debug;
	
	public function wordTest1(){
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        // 新的页面
        $section = $phpWord->addSection();
        // 简单文本
        $section->addTitle('CA重庆起止-阿联酋7天5晚', 1);
        $section->addText('阿拉伯联合酋长国（The United Arab Emirates），简称为阿联酋，位于阿拉伯半岛东部，北濒波斯湾，西北与卡塔尔为邻，西和南与沙特阿拉伯交界，东和东北与阿曼毗连海岸线长734公里，总面积83600平方公里，首都阿布扎比。!');
        // 两个换行符
        $section->addTextBreak(2);
        $section->addText('第一天：请各位贵宾出发当日15:30于重庆江北国际机场集中，搭乘中国国际航空公司下午航班（CA451（1840/2215））前往迪拜。抵达后迪拜国际机场，照眼角膜后（无须填入境卡）入境（过关时间约1.5小时）中文导游接机，后前往酒店入住休息');
   
        $section->addTextBreak();
        //超链接
        $section->addLink('http://keketour.me', '可可兔首页超链接');
        $section->addTextBreak();
        // 图片
        $section->addImage('https://www.baidu.com/img/baidu_jgylogo3.gif', array('width'=>300, 'height'=>300));
        // 保存文件
        //$writers = array('Word2007' => 'docx', 'ODText' => 'odt', 'RTF' => 'rtf', 'HTML' => 'html');
        //write($phpWord,'demo1', $writers);
		// Saving the document as OOXML file...
		$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
		$objWriter->save('f:\\test11.docx');		
		
		// Creating the new document...
		$phpWord = new \PhpOffice\PhpWord\PhpWord();
		
		/* Note: any element you append to a document must reside inside of a Section. */
		
		// Adding an empty Section to the document...
		$section = $phpWord->addSection();
		// Adding Text element to the Section having font styled by default...
		$section->addText(
			'"Learn from yesterday, live for today, hope for tomorrow. '
				. 'The important thing is not to stop questioning." '
				. '(Albert Einstein)'
		);
		
		/*
		* Note: it's possible to customize font style of the Text element you add in three ways:
		* - inline;
		* - using named font style (new font style object will be implicitly created);
		* - using explicitly created font style object.
		*/
		
		// Adding Text element with font customized inline...
		$section->addText(
			'"Great achievement is usually born of great sacrifice, '
				. 'and is never the result of selfishness." '
				. '(Napoleon Hill)',
			array('name' => 'Tahoma', 'size' => 10)
		);
		
		// Adding Text element with font customized using named font style...
		$fontStyleName = 'oneUserDefinedStyle';
		$phpWord->addFontStyle(
			$fontStyleName,
			array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
		);
		$section->addText(
			'"The greatest accomplishment is not in never falling, '
				. 'but in rising again after you fall." '
				. '(Vince Lombardi)',
			$fontStyleName
		);
		
		// Adding Text element with font customized using explicitly created font style object...
		$fontStyle = new \PhpOffice\PhpWord\Style\Font();
		$fontStyle->setBold(true);
		$fontStyle->setName('Tahoma');
		$fontStyle->setSize(13);
		$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
		$myTextElement->setFontStyle($fontStyle);
		
		// Saving the document as OOXML file...
		$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
		$objWriter->save('f:\\test12.docx');
		
		// Saving the document as ODF file...
		//$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
		//$objWriter->save('helloWorld.odt');
		//
		//// Saving the document as HTML file...
		//$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
		//$objWriter->save('helloWorld.html');
		
		/* Note: we skip RTF, because it's not XML-based and requires a different example. */
		/* Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */		
	}
	
	public function wordRead1(){
		$dirName = iconv('UTF-8','GB2312',"F:\\000_booktran\\combine\\test\\");
		$fileNames = scandir($dirName);
		foreach($fileNames as $fileName){
			if(!preg_match("/\.doc/", $fileName))
				continue;
			$realPath = $dirName.$fileName;
			echo  iconv('GB2312','UTF-8',$realPath)."</br>";
			$phpWord = \PhpOffice\PhpWord\IOFactory::load($realPath);
			$section = $phpWord->getSections();
			$elements = $section[0]->getElements();
			//var_dump($phpWord);
			//var_dump($section);
			//var_dump($elements);
			foreach($elements as $e){
				$ns = get_class($e);
				$nsArray = explode('\\', $ns);
				$elName = $nsArray[count($nsArray)-1];
				//$elName = end(explode('\\', $ns));
				if (!in_array($elName,array('PageBreak','TextRun','Text','TextBreak','Title') )){
					echo "$elName</br>";
				}
			}
		}
	}
	
	public static function exportWord_01($realPath,$textArray,$fontStyleArray=[]){
		$defaultFontStyleArray=array('name' => 'Tahoma', 'size' => 12, 'italic'=>false);
		if (empty($fontStyleArray)){
			$fontStyleArray=$defaultFontStyleArray;
		}
		if (!is_array($textArray))
			$textArray = array($textArray);
		
		$textArrayCount0 = count($textArray,0);
		$textArrayCount1 = count($textArray,1);
		$textArrayDim = ($textArrayCount0==$textArrayCount1) ? 1 : 2;
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        // 新的页面
        $section = $phpWord->addSection();
        // 简单文本
        //$section->addTitle('CA重庆起止-阿联酋7天5晚', 1);
		
		if ($textArrayDim==1){
			foreach($textArray as $text){
				$section->addText($text,$fontStyleArray);        
				$section->addTextBreak(1);
			}
		}else{
			foreach($textArray as $i1=>$textArray1){
				
				$textrun = $section->addTextRun();
				//$textrun->addText($i1);
				foreach($textArray1 as $i2=>$text){
					if ($text=="</br></br>"){
						$section->addTextBreak(0);//$textrun->addTextBreak();//$section->addTextBreak(1);
						continue;
					}
						
					if (!empty($fontStyleArray[$i1][$i2])){
						//echo "not empty</br>";
//						echo "$text</br>";
//						var_dump($fontStyleArray[$i1][$i2]);
//						echo "------------</br>";
						$textrun->addText($text,$fontStyleArray[$i1][$i2]);//$section->addText($text,$fontStyleArray[$i1][$i2]);
					}
					else
						$textrun->addText($text,$defaultFontStyleArray);//$section->addText($text,$defaultFontStyleArray);
				}
			}
		}
		
		$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
		$objWriter->save($realPath);		
	}
	
	public function testMergeFile(){
		$file1="F:\\000_booktran\\combine\\test\\test2a.docx";
		$file2="F:\\000_booktran\\combine\\test\\test2b.docx";
		$outputfile="F:\\000_booktran\\combine\\test\\test2c.docx";
		\PhpOffice\PhpWord\IOFactory::load($file1);
		\PhpOffice\PhpWord\IOFactory::load($file2);
		$myMergeFile = new MergeFile;
		$myMergeFile->joinFile($file1,$file2,$outputfile,0);
	}
}
?>